package com.zhang.backend.controller;

import com.zhang.backend.common.response.Result;
import com.zhang.backend.dto.GenealogyNodeDTO;
import com.zhang.backend.entity.Person;
import com.zhang.backend.service.GenealogyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/genealogy")
public class GenealogyController {

    @Autowired
    private GenealogyService genealogyService;

    @GetMapping("/roots/{familyTreeId}")
    public Result<List<Person>> getRootPersons(@PathVariable Long familyTreeId) {
        return Result.success(genealogyService.findRootPersons(familyTreeId));
    }

    @GetMapping("/full-tree/{rootPersonId}")
    public Result<GenealogyNodeDTO> getFullTree(@PathVariable Long rootPersonId) {
        GenealogyNodeDTO tree = genealogyService.buildTreeFromRoot(rootPersonId);
        return tree != null ? Result.success(tree) : Result.failure(404, "Not Found");
    }
}